package com.pinapel.aixml.models.xml;

import java.util.ArrayList;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class NodeID {
	/*
	 * Members
	 */
	private  List<Integer> mSequence;
	
	/**
	 * @author karloz
	 * @param  sequence 
	 */
	public NodeID(List<Integer> sequence){
		this.mSequence = sequence;
	}
	
	public NodeID(int[] sequence){
		this.mSequence = new ArrayList<Integer>();
		for (int i=0;i<sequence.length;i++){
			this.mSequence.add(sequence[i]);			
		}
	}
	
	public NodeID(DefaultMutableTreeNode tree){
		this.mSequence = new ArrayList<Integer>();
		TreeNode[] array = tree.getPath();
		for(int i = 0; i<array.length-1;i++){
			this.mSequence.add(array[i].getIndex(array[i+1]));
		}
	}
	
	/**
	 * @author karloz
	 * @param  void
	 */	
	public NodeID getTail(){		
		return new NodeID(this.mSequence.subList(1, this.mSequence.size()));
	}
	
	/**
	 * @author karloz 
	 * @return first sequence integer 
	 */
	public int getHead(){
		return this.mSequence.get(0);
	}
	
	/**
	 * @author karloz
	 * @return List<Integer>
	 */
	
	public List<Integer> getList(){
		return this.mSequence;
	}
	
	public int getLast(){
		return this.mSequence.get(this.mSequence.size()-1);
	}
	
	public String toString(){
		StringBuilder tmp = new StringBuilder();
		tmp.append("[");
		for (Integer i : this.mSequence){
			tmp.append(" "+i);
		}
		tmp.append(" ]");
		return tmp.toString();
	}
	
	public NodeID clone(){
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		for (int i=0;i<this.mSequence.size();i++){
			tmp.add(this.mSequence.get(i));
		}
		return new NodeID(tmp);
	}
	
	public boolean equals(Object nid){
		if (nid instanceof NodeID){
			if (((NodeID)nid).getList().size()!=this.getList().size()){
				return false;
			}else{ 
				return (((NodeID)nid).getList().equals(this.getList()));			
			}
		}else{
			return false;		
		}
	}
}
